package com.smugmugsoftware.airconcalc.advanced;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class AreaWizardDialog extends DialogFragment {

	private MainActivityInterface activity;

	private static final String INSTRUCTION_STRING = "\n\nEnter heights and widths as required (blank spaces will be treated as zeroes) and an algorithm will do the rest, automatically calculating transmittance and surface area.";

	private Context context;
	private int type;
	private DialogFragment thisDialog;
	private View dialogView;
	private int shadeSubType;
	private int shadeFacing;
	private int latitude;

	private int[] dims;

	public AreaWizardDialog(Context context, int type) {
		this.context = context;
		this.type = type;
		this.thisDialog = this;
	}

	public AreaWizardDialog(Context context, int type, int shadeSubType, int latitude, int[] dims) {
		this.context = context;
		this.type = type;
		this.thisDialog = this;
		this.shadeSubType = shadeSubType;
		this.dims = dims;
		this.latitude = latitude;
		this.shadeFacing = 0;
	}

	public AreaWizardDialog(Context context, int type, int shadeSubType, int latitude, int[] dims, int shadeFacing) {
		this.context = context;
		this.type = type;
		this.thisDialog = this;
		this.shadeSubType = shadeSubType;
		this.dims = dims;
		this.latitude = latitude;
		this.shadeFacing = shadeFacing;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			this.activity = (MainActivityInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (type == NewRoomFragmentInterface.CEILING_WIZARD_SELECTION) {

			builder.setItems(new String[] { "Cathedral Ceiling", "Vaulted Ceiling" }, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (which == 0) {
						thisDialog.dismiss();
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.CATHEDRAL_CEILING);
					} else if (which == 1) {
						thisDialog.dismiss();
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.VAULTED_CEILING);
					}
				}
			});
			return builder.create();
		}

		if (type == NewRoomFragmentInterface.WINDOW_WIZARD_SELECTION) {

			builder.setItems(new String[] { "Arched", "Peak/Triangular", "Circular/Oval", "Half Cathedral/Quarter Circle", "Chord/Half-Circle" }, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (which == 0) {
						thisDialog.dismiss();
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.WINDOW_ARCHED);
					} else if (which == 1) {
						thisDialog.dismiss();
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.WINDOW_TRIANGULAR);
					} else if (which == 2) {
						thisDialog.dismiss();
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.WINDOW_ELLIPSE);
					} else if (which == 3) {
						thisDialog.dismiss();
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.WINDOW_HALF_ARCH);
					} else if (which == 4) {
						thisDialog.dismiss();
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.WINDOW_HALF_ELLIPSE);
					}
				}
			});
			return builder.create();
		}

		if (type == NewRoomFragmentInterface.SHADE_WIZARD_SELECTION) {

			builder.setItems(new String[] { "Percentage Quick-Calc", "Shaded by Overhang" }, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {

					if (shadeSubType == NewRoomFragmentInterface.WINDOW_SQUARE) {
						if (which == 0) {
							thisDialog.dismiss();
							activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.SHADE_PERCENTAGE, shadeSubType, dims);
						} else if (which == 1) {
							thisDialog.dismiss();
							activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.SHADE_OVERHANG, shadeSubType, dims);
						}
					} else if (shadeSubType == NewRoomFragmentInterface.WINDOW_AREA) {
						if (which == 0) {
							thisDialog.dismiss();
							activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.SHADE_PERCENTAGE, shadeSubType, dims);
						} else if (which == 1) {
							thisDialog.dismiss();
							activity.shadeAreaError();
						}						
					}
				}
			});
			return builder.create();
		}

		if (type == NewRoomFragmentInterface.CATHEDRAL_CEILING) {
			dialogView = inflater.inflate(R.layout.cathedral_wizard_dialog, null);
			builder.setTitle("Cathedral Ceiling Wizard");

		} else if (type == NewRoomFragmentInterface.VAULTED_CEILING) {
			builder.setTitle("Vaulted Ceiling Wizard");
			dialogView = inflater.inflate(R.layout.vaulted_wizard_dialog, null);

		} else if (type == NewRoomFragmentInterface.WINDOW_ARCHED) {
			builder.setTitle("Arched Window/Door Wizard");
			dialogView = inflater.inflate(R.layout.window_area_wizard_dialog, null);

			((ImageView) dialogView.findViewById(R.id.imageView1))
			.setImageDrawable(getResources().getDrawable(R.drawable.window_arched));
			((TextView) dialogView.findViewById(R.id.help)).setText("Arched windows feature straight flat sides perpendicular to the floor and a symmetrical curved peak.  This peak may be either circular or elliptical." + INSTRUCTION_STRING);

		} else if (type == NewRoomFragmentInterface.WINDOW_TRIANGULAR) {
			builder.setTitle("Peak/Triangular Wizard");
			dialogView = inflater.inflate(R.layout.window_area_wizard_dialog, null);

			((ImageView) dialogView.findViewById(R.id.imageView1))
			.setImageDrawable(getResources().getDrawable(R.drawable.window_triangle));
			((TextView) dialogView.findViewById(R.id.help)).setText("Peaked windows feature straight flat sides which are perpendicular to the floor.  At the top, the two sides turn at equal angles toward a center point or peak." + INSTRUCTION_STRING);

		} else if (type == NewRoomFragmentInterface.WINDOW_HALF_ARCH) {
			builder.setTitle("Half Cathedral/Quarter Circle Wizard");
			dialogView = inflater.inflate(R.layout.window_area_wizard_dialog, null);

			((ImageView) dialogView.findViewById(R.id.imageView1))
			.setImageDrawable(getResources().getDrawable(R.drawable.window_half_arched));
			((TextView) dialogView.findViewById(R.id.help)).setText("Half Cathedral windows are identical in design to arched windows, but are cut in half along a line from the apex of the peak to the center of the base.  Quarter circle windows are identical in design, but have no sides (enter zero for the h2 measurement)" + INSTRUCTION_STRING);

		} else if (type == NewRoomFragmentInterface.WINDOW_ELLIPSE) {
			builder.setTitle("Circular/Oval Wizard");
			dialogView = inflater.inflate(R.layout.window_simple_area_wizard_dialog, null);

			((ImageView) dialogView.findViewById(R.id.imageView1))
			.setImageDrawable(getResources().getDrawable(R.drawable.window_ellipse));
			((TextView) dialogView.findViewById(R.id.help)).setText("Oval windows are often used ornamentally in architecture.  Circular windows are simply ovals with equal widths and heights." + INSTRUCTION_STRING);

		} else if (type == NewRoomFragmentInterface.WINDOW_HALF_ELLIPSE) {
			builder.setTitle("Chord/Half-Ellipse Wizard");
			dialogView = inflater.inflate(R.layout.window_simple_area_wizard_dialog, null);

			((ImageView) dialogView.findViewById(R.id.imageView1))
			.setImageDrawable(getResources().getDrawable(R.drawable.window_half_ellipse));
			((TextView) dialogView.findViewById(R.id.help)).setText("Chord or half-ellipse windows are often used as accents and toppers for other design elements, though they are occasionally found alone.  Half-circle windows are half-ellipses with equal widths and heights." + INSTRUCTION_STRING);

		} else if (type == NewRoomFragmentInterface.SHADE_OVERHANG) {
			builder.setTitle("Shade From Overhang Wizard");
			dialogView = inflater.inflate(R.layout.shade_wizard_dialog, null);

			((ImageView) dialogView.findViewById(R.id.imageView1))
			.setImageDrawable(getResources().getDrawable(R.drawable.overhang));
			((TextView) dialogView.findViewById(R.id.help)).setText("Shade from exterior features like overhangs can have a significant impact on the heat gain of a room, particularly in bright climates.\n\nTo calculate the shaded portion of a window, enter the length of the overhang and the distance between the top of the window and the overhang in the appropriate spaces below.  If any spaces are left blank, the program will assume that there is no shade.\n\nNOTE: Northerly facing windows will receive NO shade in the northern hemisphere, and vice versa in the southern hemisphere.");
			((CompassSelector) dialogView.findViewById(R.id.facing_spinner)).setSelection(shadeFacing);

			if (dims.length == 3) {
				((TextView) dialogView.findViewById(R.id.h2)).setText(String.valueOf(dims[0] + dims[1]));
			} else if (dims.length == 2) {
				((TextView) dialogView.findViewById(R.id.h2)).setText(String.valueOf(dims[0]));
			}

		} else if (type == NewRoomFragmentInterface.SHADE_PERCENTAGE) {
			builder.setTitle("Shade Percentage Quick-Calc");
			dialogView = inflater.inflate(R.layout.shade_percent_wizard_dialog, null);

			((ImageView) dialogView.findViewById(R.id.home))
			.setImageDrawable(getResources().getDrawable(R.drawable.overhang_blank));
			((TextView) dialogView.findViewById(R.id.help)).setText("Shade can have a significant impact on the heat gain of a room, particularly in bright climates.  While this app uses various forms of vegetation to illustrate the presence of shade, it can come from a number of sources.\n\nDetermining an arbitrary amount shade with high precision would require a lenghty case study.  Instead, you can use this slider to estimate the amount of shade this window receives in the afternoon.");
		}

		if (shadeSubType == NewRoomFragmentInterface.WINDOW_ARCHED
				|| shadeSubType == NewRoomFragmentInterface.WINDOW_ELLIPSE
				|| shadeSubType == NewRoomFragmentInterface.WINDOW_HALF_ARCH
				|| shadeSubType == NewRoomFragmentInterface.WINDOW_HALF_ELLIPSE
				|| shadeSubType == NewRoomFragmentInterface.WINDOW_TRIANGULAR) {
			if (dims.length == 3) {
				((TextView) dialogView.findViewById(R.id.h2)).setText(String.valueOf(dims[0] + dims[1]));
			} else if (dims.length == 2) {
				((TextView) dialogView.findViewById(R.id.h2)).setText(String.valueOf(dims[0]));
			}
		}


		final CompassSelector compass = (CompassSelector) dialogView.findViewById(R.id.facing_spinner);
		final CheckBox shadeCheckBox = (CheckBox) dialogView.findViewById(R.id.shade_checkbox);
		final LinearLayout headingWrapper = (LinearLayout) dialogView.findViewById(R.id.explainThisHeadingWrapper);
		final TextView plusMinus = (TextView) dialogView.findViewById(R.id.explainThisExpander);
		final TextView help = (TextView) dialogView.findViewById(R.id.help);

		plusMinus.setText("+");
		help.setVisibility(View.GONE);

		headingWrapper.setOnClickListener(new android.view.View.OnClickListener() {
			public void onClick(View v) {
				if (plusMinus.getText().equals("+")) {
					plusMinus.setText("-");
					help.setVisibility(View.VISIBLE);
				} else {
					plusMinus.setText("+");
					help.setVisibility(View.GONE);
				}
			}
		});

		builder.setNegativeButton("Cancel", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				thisDialog.dismiss();
			}
		});

		builder.setPositiveButton("Done", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

				double area = 0;

				if (type == NewRoomFragmentInterface.CATHEDRAL_CEILING) { 
					EditText[] editTextArray = new EditText[12];
					int[] returnList = new int[12];
					int[] combinedList = new int[6];

					editTextArray[0] = (EditText) dialogView.findViewById(R.id.h1_ft);
					editTextArray[1] = (EditText) dialogView.findViewById(R.id.h1_in);
					editTextArray[2] = (EditText) dialogView.findViewById(R.id.h2_ft);
					editTextArray[3] = (EditText) dialogView.findViewById(R.id.h2_in);
					editTextArray[4] = (EditText) dialogView.findViewById(R.id.w1_ft);
					editTextArray[5] = (EditText) dialogView.findViewById(R.id.w1_in);
					editTextArray[6] = (EditText) dialogView.findViewById(R.id.w2_ft);
					editTextArray[7] = (EditText) dialogView.findViewById(R.id.w2_in);
					editTextArray[8] = (EditText) dialogView.findViewById(R.id.w3_ft);
					editTextArray[9] = (EditText) dialogView.findViewById(R.id.w3_in);
					editTextArray[10] = (EditText) dialogView.findViewById(R.id.room_length_ft);
					editTextArray[11] = (EditText) dialogView.findViewById(R.id.room_length_in);

					for (int i = 0; i < editTextArray.length; i++) {
						returnList[i] = zeroCheck(editTextArray[i]);
					}

					for (int i = 0; i < returnList.length; i = i + 2) {
						returnList[i] = returnList[i] * 12;
					}

					for (int i = 0; i < returnList.length; i = i + 2) {
						combinedList[i / 2] = returnList[i] + returnList[i + 1];
					}

					area = area + combinedList[4];
					area = area + java.lang.Math.sqrt(java.lang.Math.pow(java.lang.Math.abs(combinedList[1] - combinedList[0]), 2)
							+ java.lang.Math.pow(combinedList[3], 2));
					area = area * 2;
					area = area + combinedList[2];
					area = area * combinedList[5];
					area = area / 144;
					activity.getDoubleFromDialog(type, area);

				} else if (type == NewRoomFragmentInterface.VAULTED_CEILING) {
					EditText[] editTextArray = new EditText[20];
					int[] returnList = new int[20];
					int[] combinedList = new int[10];

					editTextArray[0] = (EditText) dialogView.findViewById(R.id.h1_ft);
					editTextArray[1] = (EditText) dialogView.findViewById(R.id.h1_in);
					editTextArray[2] = (EditText) dialogView.findViewById(R.id.h2_ft);
					editTextArray[3] = (EditText) dialogView.findViewById(R.id.h2_in);
					editTextArray[4] = (EditText) dialogView.findViewById(R.id.h3_ft);
					editTextArray[5] = (EditText) dialogView.findViewById(R.id.h3_in);
					editTextArray[6] = (EditText) dialogView.findViewById(R.id.h4_ft);
					editTextArray[7] = (EditText) dialogView.findViewById(R.id.h4_in);
					editTextArray[8] = (EditText) dialogView.findViewById(R.id.w1_ft);
					editTextArray[9] = (EditText) dialogView.findViewById(R.id.w1_in);
					editTextArray[10] = (EditText) dialogView.findViewById(R.id.w2_ft);
					editTextArray[11] = (EditText) dialogView.findViewById(R.id.w2_in);
					editTextArray[12] = (EditText) dialogView.findViewById(R.id.w3_ft);
					editTextArray[13] = (EditText) dialogView.findViewById(R.id.w3_in);
					editTextArray[14] = (EditText) dialogView.findViewById(R.id.w4_ft);
					editTextArray[15] = (EditText) dialogView.findViewById(R.id.w4_in);
					editTextArray[16] = (EditText) dialogView.findViewById(R.id.w5_ft);
					editTextArray[17] = (EditText) dialogView.findViewById(R.id.w5_in);
					editTextArray[18] = (EditText) dialogView.findViewById(R.id.room_length_ft);
					editTextArray[19] = (EditText) dialogView.findViewById(R.id.room_length_in);

					for (int i = 0; i < editTextArray.length; i++) {
						returnList[i] = zeroCheck(editTextArray[i]);
					}

					for (int i = 0; i < returnList.length; i = i + 2) {
						returnList[i] = returnList[i] * 12;
					}

					for (int i = 0; i < returnList.length; i = i + 2) {
						combinedList[i / 2] = returnList[i] + returnList[i + 1];
					}

					area = area + combinedList[4] + combinedList[6] + combinedList[8];
					area = area + java.lang.Math.sqrt(java.lang.Math.pow(java.lang.Math.abs(combinedList[1] - combinedList[0]), 2)
							+ java.lang.Math.pow(combinedList[5], 2));
					area = area + java.lang.Math.sqrt(java.lang.Math.pow(java.lang.Math.abs(combinedList[2] - combinedList[3]), 2)
							+ java.lang.Math.pow(combinedList[7], 2));
					area = area * combinedList[9];
					area = area / 144;
					activity.getDoubleFromDialog(type, area);

				} else if (type == NewRoomFragmentInterface.WINDOW_ARCHED) {
					EditText[] editTextArray = new EditText[3];
					int[] returnList = new int[3];

					editTextArray[0] = (EditText) dialogView.findViewById(R.id.h1);
					editTextArray[1] = (EditText) dialogView.findViewById(R.id.h2);
					editTextArray[2] = (EditText) dialogView.findViewById(R.id.w1);

					for (int i = 0; i < editTextArray.length; i++) {
						returnList[i] = zeroCheck(editTextArray[i]);
					}

					if (shadeCheckBox.isChecked()) {
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.SHADE_OVERHANG, type, returnList);
					} else {
						area = area + (returnList[1] * returnList[2]);
						area = area + ((java.lang.Math.PI * returnList[0] * returnList[2]) / 4);
						area = area / 144;
						activity.getDoubleFromDialog(type, area);
					}
				} else if (type == NewRoomFragmentInterface.WINDOW_TRIANGULAR) {
					EditText[] editTextArray = new EditText[3];
					int[] returnList = new int[3];

					editTextArray[0] = (EditText) dialogView.findViewById(R.id.h1);
					editTextArray[1] = (EditText) dialogView.findViewById(R.id.h2);
					editTextArray[2] = (EditText) dialogView.findViewById(R.id.w1);

					for (int i = 0; i < editTextArray.length; i++) {
						returnList[i] = zeroCheck(editTextArray[i]);
					}

					if (shadeCheckBox.isChecked()) {
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.SHADE_OVERHANG, type, returnList);
					} else {
						area = area + (returnList[1] * returnList[2]);
						area = area + ((returnList[0] * returnList[2]) / 2);
						area = area / 144;
						activity.getDoubleFromDialog(type, area);
					}

				} else if (type == NewRoomFragmentInterface.WINDOW_HALF_ARCH) {
					EditText[] editTextArray = new EditText[3];
					int[] returnList = new int[3];

					editTextArray[0] = (EditText) dialogView.findViewById(R.id.h1);
					editTextArray[1] = (EditText) dialogView.findViewById(R.id.h2);
					editTextArray[2] = (EditText) dialogView.findViewById(R.id.w1);

					for (int i = 0; i < editTextArray.length; i++) {
						returnList[i] = zeroCheck(editTextArray[i]);
					}

					if (shadeCheckBox.isChecked()) {
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.SHADE_OVERHANG, type, returnList);
					} else {
						area = area + (returnList[1] * returnList[2]);
						area = area + ((java.lang.Math.PI * returnList[0] * returnList[2]) / 4);
						area = area / 144;
						activity.getDoubleFromDialog(type, area);
					}

				} else if (type == NewRoomFragmentInterface.WINDOW_ELLIPSE) {
					EditText[] editTextArray = new EditText[2];
					int[] returnList = new int[2];

					editTextArray[0] = (EditText) dialogView.findViewById(R.id.h1);
					editTextArray[1] = (EditText) dialogView.findViewById(R.id.w1);

					for (int i = 0; i < editTextArray.length; i++) {
						returnList[i] = zeroCheck(editTextArray[i]);
					}

					if (shadeCheckBox.isChecked()) {
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.SHADE_OVERHANG, type, returnList);
					} else {
						area = area + ((java.lang.Math.PI * returnList[0] * returnList[1]) / 4);
						area = area / 144;
						activity.getDoubleFromDialog(type, area);
					}

				} else if (type == NewRoomFragmentInterface.WINDOW_HALF_ELLIPSE) {
					EditText[] editTextArray = new EditText[2];
					int[] returnList = new int[2];

					editTextArray[0] = (EditText) dialogView.findViewById(R.id.h1);
					editTextArray[1] = (EditText) dialogView.findViewById(R.id.w1);

					for (int i = 0; i < editTextArray.length; i++) {
						returnList[i] = zeroCheck(editTextArray[i]);
					}

					if (shadeCheckBox.isChecked()) {
						activity.displayAreaWizardDialog(context, NewRoomFragmentInterface.SHADE_OVERHANG, type, returnList);
					} else {
						area = area + ((java.lang.Math.PI * returnList[0] * returnList[1]) / 4);
						area = area / 144;
						activity.getDoubleFromDialog(type, area);
					}

				} else if (type == NewRoomFragmentInterface.SHADE_OVERHANG) {
					EditText[] editTextArray = new EditText[2];
					int[] returnList = new int[2];
					double shadeArea = 0;
					int getColumn = 0;

					int facing = compass.getSelectedItemPosition();
					if (latitude >= 0) {
						if (facing == 2 || facing == 6) {
							getColumn = 1;
						} else if (facing == 3 || facing == 5) {
							getColumn = 2;
						} else if (facing == 4) {
							getColumn = 3;
						}
					} else if (latitude < 0) {
						if (facing == 2 || facing == 6) {
							getColumn = 1;
						} else if (facing == 1 || facing == 7) {
							getColumn = 2;
						} else if (facing == 0) {
							getColumn = 3;
						}						
					}
					
					if (latitude < 0) {
						latitude = latitude * -1;
					}
					
					TableParser shadeParser = new TableParser(context, R.raw.shadedata);

					double shadeMultiplier = 0;
					if (getColumn != 0) {
						shadeMultiplier = Double.parseDouble(shadeParser.getDataPoint(String.valueOf(latitude), 
								String.valueOf(NewRoomFragmentInterface.WINDOW_SHADE), getColumn));
					}

					editTextArray[0] = (EditText) dialogView.findViewById(R.id.h1);
					editTextArray[1] = (EditText) dialogView.findViewById(R.id.w1);

					for (int i = 0; i < editTextArray.length; i++) {
						returnList[i] = zeroCheck(editTextArray[i]);
					}

					double shadeLine = returnList[1] * shadeMultiplier;
					double windowShadedY = shadeLine - returnList[0];

					if (shadeSubType == NewRoomFragmentInterface.WINDOW_ARCHED) {

						area = area + (dims[1] * dims[2]);
						area = area + ((java.lang.Math.PI * dims[0] * dims[2]) / 4);
						area = area / 144;

						if (windowShadedY > 0) {
							if (windowShadedY >= dims[0] + dims[1]) {
								shadeArea = area;

							} else if (windowShadedY >= dims[0]) {
								shadeArea = shadeArea + ((java.lang.Math.PI * dims[0] * dims[2]) / 4);
								activity.getDoubleFromDialog(type, area);
								shadeArea = shadeArea + (dims[1] * (windowShadedY - dims[0]));

							} else if (windowShadedY < dims[0]) {
								double h = dims[0];
								double w = dims[1] / 2;
								double a = windowShadedY;
								double b = (w/h) * java.lang.Math.sqrt(java.lang.Math.pow(h, 2) - (java.lang.Math.pow(a, 2)));
								double x = b;
								shadeArea = shadeArea + (h / (2* w)) * (x * java.lang.Math.sqrt(java.lang.Math.pow(w, 2) - java.lang.Math.pow(x, 2)) + java.lang.Math.pow(w, 2) * java.lang.Math.asin(x/w));
								x = 0;
								shadeArea = shadeArea + (h / (2* w)) * (x * java.lang.Math.sqrt(java.lang.Math.pow(w, 2) - java.lang.Math.pow(x, 2)) + java.lang.Math.pow(w, 2) * java.lang.Math.asin(x/w));
								shadeArea = shadeArea - (a * b);
								shadeArea = shadeArea * 2;
							}
						} else {
							shadeArea = 0;
						}

					} else if (shadeSubType == NewRoomFragmentInterface.WINDOW_ELLIPSE) {
						area = area + ((java.lang.Math.PI * dims[0] * dims[1]) / 4);
						area = area / 144;

						if (windowShadedY > 0) {
							if (windowShadedY >= dims[0]) {
								shadeArea = area;
							} else if (windowShadedY < dims[0]) {

								double h = dims[0] / 2;
								double w = dims[1] / 2;
								double a = windowShadedY;
								double b = (w/h) * java.lang.Math.sqrt(java.lang.Math.pow(h, 2) - (java.lang.Math.pow(a, 2)));
								double x = b;
								shadeArea = shadeArea + (h / (2* w)) * (x * java.lang.Math.sqrt(java.lang.Math.pow(w, 2) - java.lang.Math.pow(x, 2)) + java.lang.Math.pow(w, 2) * java.lang.Math.asin(x/w));
								x = 0;
								shadeArea = shadeArea - (h / (2* w)) * (x * java.lang.Math.sqrt(java.lang.Math.pow(w, 2) - java.lang.Math.pow(x, 2)) + java.lang.Math.pow(w, 2) * java.lang.Math.asin(x/w));
								shadeArea = shadeArea - (a * b);

								if (windowShadedY > dims[0] / 2) {
									shadeArea = shadeArea + (a * b);
									x = w;
									shadeArea = shadeArea + (h / (2* w)) * (x * java.lang.Math.sqrt(java.lang.Math.pow(w, 2) - java.lang.Math.pow(x, 2)) + java.lang.Math.pow(w, 2) * java.lang.Math.asin(x/w));
									x = b;
									shadeArea = shadeArea - (h / (2* w)) * (x * java.lang.Math.sqrt(java.lang.Math.pow(w, 2) - java.lang.Math.pow(x, 2)) + java.lang.Math.pow(w, 2) * java.lang.Math.asin(x/w));
								}
								shadeArea = shadeArea * 2;
							}
						} else {
							shadeArea = 0;
						}
					} else if (shadeSubType == NewRoomFragmentInterface.WINDOW_HALF_ARCH) {
						if (windowShadedY >= dims[0]) {

							area = area + (dims[1] * dims[2]);
							area = area + ((java.lang.Math.PI * dims[0] * dims[2]) / 4);
							area = area / 144;

							if (windowShadedY > 0) {
								if (windowShadedY >= dims[0] + dims[1]) {
									shadeArea = area;

								} else if (windowShadedY >= dims[0]) {
									shadeArea  = shadeArea  + ((java.lang.Math.PI * dims[0] * dims[2]) / 4);
									shadeArea = shadeArea  + (dims[1] * (windowShadedY - dims[0]));

								} else if (windowShadedY < dims[0]) {
									double h = dims[0];
									double w = dims[2];
									double a = windowShadedY;
									double b = (w/h) * java.lang.Math.sqrt(java.lang.Math.pow(h, 2) - (java.lang.Math.pow(a, 2)));
									double x = b;
									shadeArea = shadeArea + (h / (2* w)) * (x * java.lang.Math.sqrt(java.lang.Math.pow(w, 2) - java.lang.Math.pow(x, 2)) + java.lang.Math.pow(w, 2) * java.lang.Math.asin(x/w));
									x = 0;
									shadeArea = shadeArea - (h / (2* w)) * (x * java.lang.Math.sqrt(java.lang.Math.pow(w, 2) - java.lang.Math.pow(x, 2)) + java.lang.Math.pow(w, 2) * java.lang.Math.asin(x/w));
									shadeArea = shadeArea - (a * b);
								}
							}
						}
					} else if (shadeSubType == NewRoomFragmentInterface.WINDOW_HALF_ELLIPSE) {
						area = area + (java.lang.Math.PI * dims[0] * dims[1]);
						area = area / 144;
						if (windowShadedY > 0) {
							if (windowShadedY >= dims[0]) {
								shadeArea = area;

							} else if (windowShadedY < dims[0]) {
								double h = dims[0];
								double w = dims[1] / 2;
								double a = windowShadedY;
								double b = (w/h) * java.lang.Math.sqrt(java.lang.Math.pow(h, 2) - (java.lang.Math.pow(a, 2)));
								double x = b;
								shadeArea = shadeArea + (h / (2* w)) * (x * java.lang.Math.sqrt(java.lang.Math.pow(w, 2) - java.lang.Math.pow(x, 2)) + java.lang.Math.pow(w, 2) * java.lang.Math.asin(x/w));
								x = 0;
								shadeArea = shadeArea - (h / (2* w)) * (x * java.lang.Math.sqrt(java.lang.Math.pow(w, 2) - java.lang.Math.pow(x, 2)) + java.lang.Math.pow(w, 2) * java.lang.Math.asin(x/w));
								shadeArea = shadeArea - (a * b);
								shadeArea = shadeArea * 2;
							}
						} else {
							shadeArea = 0;
						}
					} else if (shadeSubType == NewRoomFragmentInterface.WINDOW_TRIANGULAR) {
						area = area + (dims[1] * dims[2]);
						area = area + ((dims[0] * dims[2]) / 2);
						area = area / 144;
						if (windowShadedY > 0) {
							if (windowShadedY >= dims[0] + dims[1]) {
								shadeArea = area;

							} else if (windowShadedY >= dims[0]) {

								shadeArea = shadeArea + ((dims[0] * dims[2]) / 2);
								shadeArea = shadeArea + (dims[1] * (windowShadedY - dims[0]));

							} else if (windowShadedY < dims[0]) {
								double h = dims[0];
								double w = dims[1] / 2;
								double a = windowShadedY;

								shadeArea = (w / (2 * h)) * (java.lang.Math.pow(a, 2) - (2 * a * h) - java.lang.Math.pow(h, w));
							}
						} else {
							shadeArea = 0;
						}
					} else if (shadeSubType == NewRoomFragmentInterface.WINDOW_SQUARE) {

						area = area + (dims[0] * dims[1]);
						area = area / 144;
						shadeArea = shadeArea + (windowShadedY * dims[1]);
					}

					if (shadeArea > area) {
						shadeArea = area;
					}

					if (shadeArea < 0) {
						shadeArea = 0;

					}

					activity.getDataFromDialog(shadeSubType, new double[] {area, shadeArea}, compass.getSelectedItemPosition());

				} else if (type == NewRoomFragmentInterface.SHADE_PERCENTAGE) {
					double shadeArea = 0;

					if (shadeSubType == NewRoomFragmentInterface.WINDOW_SQUARE) {
						area = area + (dims[0] * dims[1]);
						area = area / 144;
					} else if (shadeSubType == NewRoomFragmentInterface.WINDOW_AREA) {
						area = area + dims[0];
					}

					shadeArea = area * ((double) (((SeekBar) dialogView.findViewById(R.id.shade_seekbar)).getProgress()) / 100);

					if (shadeArea > area) {
						shadeArea = area;
					}

					if (shadeArea < 0) {
						shadeArea = 0;
					}

					activity.getDataFromDialog(shadeSubType, new double[] {area, shadeArea});
				}
				thisDialog.dismiss();
			}

			private int zeroCheck(EditText e) {
				String reader = e.getText().toString();
				if (reader == null || reader.length() == 0 || Integer.valueOf(reader) == 0) {
					return 0;
				} else {
					return Integer.parseInt(reader.toString());
				}
			}
		});

		builder.setView(dialogView);
		final AlertDialog builtDialog = builder.create();

		if (type == NewRoomFragmentInterface.WINDOW_ARCHED
				|| type == NewRoomFragmentInterface.WINDOW_ELLIPSE
				|| type == NewRoomFragmentInterface.WINDOW_HALF_ARCH
				|| type == NewRoomFragmentInterface.WINDOW_HALF_ELLIPSE
				|| type == NewRoomFragmentInterface.WINDOW_TRIANGULAR) {

			shadeCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						Button okButton = (Button) builtDialog.getButton(AlertDialog.BUTTON_POSITIVE);
						okButton.setText("Shaded Area Wizard");
					} else {
						Button okButton = (Button) builtDialog.getButton(AlertDialog.BUTTON_POSITIVE);
						okButton.setText("Done");
					}
				}
			});
		} else if (type == NewRoomFragmentInterface.SHADE_PERCENTAGE) {
			final ImageView tree = (ImageView) dialogView.findViewById(R.id.tree);
			final TextView shadePercent = (TextView) dialogView.findViewById(R.id.shade_percent);
			final SeekBar shadeBar = (SeekBar) dialogView.findViewById(R.id.shade_seekbar);
			tree.setImageDrawable(getResources().getDrawable(R.drawable.tree1));
			tree.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
			android.widget.RelativeLayout.LayoutParams treeParams = (android.widget.RelativeLayout.LayoutParams) tree.getLayoutParams();
			int maxY = tree.getDrawable().getIntrinsicHeight();
			int maxX = tree.getDrawable().getIntrinsicWidth();
			double multiplier = ((double) (35)) / (100 + 35);
			treeParams.height = (int) (maxY * multiplier);
			treeParams.width = (int) (maxX * multiplier);
			tree.setLayoutParams(treeParams);

			shadePercent.setText("0%");
			shadeBar.setMax(100);
			shadeBar.setProgress(0);
			shadeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onStopTrackingTouch(SeekBar seekBar) {}
				public void onStartTrackingTouch(SeekBar seekBar) {}

				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					int maxY = 0;
					int maxX = 0;
					if (progress >= 0 && progress < 35) {
						tree.setImageDrawable(getResources().getDrawable(R.drawable.tree1));
						maxY = tree.getDrawable().getIntrinsicHeight();
						maxX = tree.getDrawable().getIntrinsicWidth();
					} else if (progress >= 35 && progress < 70) {
						tree.setImageDrawable(getResources().getDrawable(R.drawable.tree2));						
						maxY = tree.getDrawable().getIntrinsicHeight();
						maxX = tree.getDrawable().getIntrinsicWidth();
					} else if (progress >= 70) {
						tree.setImageDrawable(getResources().getDrawable(R.drawable.tree3));
						maxY = tree.getDrawable().getIntrinsicHeight();
						maxX = tree.getDrawable().getIntrinsicWidth();
					}

					android.widget.RelativeLayout.LayoutParams treeParams = (android.widget.RelativeLayout.LayoutParams) tree.getLayoutParams();
					double multiplier = ((double) (progress + 35)) / (100 + 35);
					treeParams.height = (int) (maxY * multiplier);
					treeParams.width = (int) (maxX * multiplier);
					tree.setLayoutParams(treeParams);
					shadePercent.setText(String.valueOf(progress) + "%");
				}
			});
		}

		return builtDialog;
	}
}
